//
//  KVOViewController.m
//  BlocksKitSample
//
//  Created by 李勇 on 2018/10/13.
//  Copyright © 2018年 李勇. All rights reserved.
//

#import "KVOViewController.h"
#import "Utility.h"
#import "KVOBean.h"
@import BlocksKit;

@interface KVOViewController (){
    UILabel *show;
    UIButton *add;
    KVOBean *bean;
    NSString *token;
}

@end

@implementation KVOViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    bean = [[KVOBean alloc] init];
    void(^observeBlock)(id) = ^(id obj) {
        [self->show setText:[NSString stringWithFormat:@"%d", self->bean.num]];
    };
    token = [self bk_addObserverForKeyPath:@"bean.num" task:observeBlock];
    self.view.backgroundColor = [UIColor whiteColor];
    show = [[UILabel alloc] initWithFrame:CGRectZero];
    
    [self.view addSubview:show];
    add = [UIButton buttonWithType:UIButtonTypeSystem];
    [add addTarget:self action:@selector(tap) forControlEvents:UIControlEventTouchUpInside];
    [add setTitle:@"add" forState:UIControlStateNormal];
    [self.view addSubview:add];
    [self initLayout];
    [bean setNum:0];
}

-(void)dealloc{
    [self bk_removeObserversWithIdentifier:token];
}

-(void)initLayout{
    PREPCONSTRAINTS(show);
    CENTER_VIEW(self.view, show);
    PREPCONSTRAINTS(add);
    CENTER_VIEW_H(self.view, add);
    ALIGN_VIEW_BOTTOM_CONSTANT(self.view,add,-60);
}

-(void)tap{
    [bean setNum:bean.num+1];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
